import {Component, OnInit, ViewChild} from '@angular/core';
import {RatingModalViewComponent} from "../rating-modal-view/rating-modal-view.component";
import {CashierModalViewComponent} from "../cashier-modal-view/cashier-modal-view.component";
import {PageParam} from "../../model/params.model";
import {COMPLAINT} from "../../service/complaint-api";
import {WfToast} from "../../utils/toast.utils";
import {FormGroup, FormControl, FormBuilder} from "@angular/forms";

@Component({
  selector: 'app-my-suggestion-view',
  templateUrl: './my-suggestion-view.component.html',
  styleUrls: ['./my-suggestion-view.component.scss']
})
export class MySuggestionViewComponent implements OnInit {

  @ViewChild(RatingModalViewComponent) public ratingModal:any;
  @ViewChild(CashierModalViewComponent) public cashierModal:any;
  public isAdvice:boolean=true;
  public isComplaint:boolean=false;
  pageParam:PageParam;
  complaintCategory:any=[];
  complaintCode:any;
  orderList = [];
  pagination={
    page:1,
    totalPage:1
  }
  data = {
    category:undefined,
    content:undefined
  }
  sForm: FormGroup;




  constructor(public COMPLAINT:COMPLAINT,public WFTOAST:WfToast,private fb: FormBuilder,) {
    this.sForm = fb.group({
      datacategory: '',
      content:''
    });
  }

  showAdvice(){
    if(this.isComplaint==true){
      this.isComplaint=false;
    }
    this.isAdvice = true;
  }
  showComplaint(){
    if(this.isAdvice==true){
      this.isAdvice=false;
    }
    this.isComplaint = true;
  }
  showRatingModalView(){
    this.ratingModal.showModal();
  }
  showCashierModalView(){
    this.cashierModal.showModal();
  }


  ngOnInit() {
    this.pageParam = new PageParam()
    this.pageParam.size = 4;
    this.queryList()
    this.loadCategory()
  }
  onPageChanged(v){
    console.log('onPageChanged',v.data.page)
    this.pagination.page = v.data.page
    this.pageParam.page = this.pagination.page-1
    this.queryList()
  }
  queryList() {
    this.COMPLAINT.getComplaint(v => {
      this.orderList = v.content
      this.pagination.totalPage = v.totalPages;
    },this.pageParam);
  }
  loadCategory(){
    this.COMPLAINT.getComplaintCategory({},(res:any)=>{
      console.log(res);
      let list:any = [];
      for(let item of res){
        let data:any={
          code:item.code,
          name:item.name
        }
        list.push(data);
      }
      this.complaintCategory = list;
    })
  }

  sumbitComplaint(){
    this.COMPLAINT.postComplaint(this.data,(res:any)=>{
      this.WFTOAST.msg("您的投诉已经成功提交,青果客服会尽快和您联系!")
    })
  }
}
